import isNumber from 'lodash/isNumber';
const state={
    userInfo:{
        username:'',
        authorization:'',
        authBtn:[],
        authMenuTitle:[],
        balance:0
    },
    chatStatus:1,
    themeImg:require('@/assets/img/theme/star.jpg'),
    themeColor:'#409EFF',
    theme:'theme-default',
    authList:{},
    socketInfo:{},
    hideOrderMenus:{},
    starOrderMenus:{},
    routerMap:{},
}
const mutations={
    SET_CHAT_STATUS: (state, data) => {
        state.chatStatus=data
    },
    SET_USER_AUTHMENUTITLE: (state, data) => {
        state.userInfo.authMenuTitle=data
    },
    SET_SOCKET_INFO: (state, data) => {
        //来自谁的
        if(/groupChat/.test(data.to || data.from)){
            const from=`groupChat_${state.userInfo.username}`
            //群组
            if(state.socketInfo[from]){
                let unreadCount=isNumber(data.unreadCount)?data.unreadCount:state.socketInfo[from].unreadCount+1
                state.socketInfo[from].unreadCount=unreadCount
                state.socketInfo[from].endMsg=data.msg
            }else {
                state.socketInfo[from]= {endMsg:data.msg,unreadCount:data.unreadCount || 0}
            }
        }else {
            if(state.socketInfo[data.from]){
                let unreadCount=isNumber(data.unreadCount)?data.unreadCount:state.socketInfo[data.from].unreadCount+1
                state.socketInfo[data.from].unreadCount=unreadCount
                state.socketInfo[data.from].endMsg=data.msg
            }else {
                state.socketInfo[data.from]= {endMsg:data.msg,unreadCount:data.unreadCount || 0}
            }
        }
    },
    SET_THEME_BG: (state, data) => {
        state.themeImg=/\?/.test(data)?data:data+`?${new Date().getTime()}`
    },
    SET_THEME: (state, data) => {
        state.theme=data
    },
    SET_THEME_COLOR: (state, data) => {
        state.themeColor=data
    },
    SET_USER_AUTHBTN: (state, data) => {
        state.userInfo.authBtn=data
    },
    SET_AUTH_LIST: (state, data) => {
        state.authList=data
    },
    SET_HIDE_MENUS: (state, data) => {
        state.hideOrderMenus=data
    },
    SET_STAR_MENUS: (state, data) => {
        state.starOrderMenus=data
    },
    SET_ROUTER_MAP(state,data){
        state.routerMap=data
    },
    SET_ORDER_TIME_MAP: (state, data) => {
        state.orderTimeMap=data
    },
    SET_LOGIN_OUT: (state, data) => {
        Object.assign(state.userInfo,data)
    },
    SET_USER_INFO: (state, data) => {
        state.userInfo=data
    },
}
const actions={
    loginOut({ commit }) {
        commit('SET_LOGIN_OUT',{
            authorization:''
        })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
